﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace PhotoManager
{
    /// <summary>
    /// Keeps track of the user's preference of folders.
    /// </summary>
    public class FolderPrecedence
    {
        // "directory1>directory2" => true (to mimic HashSet)
        Dictionary<string, bool> store = new Dictionary<string, bool>();

        Dictionary<string, int> priority = new Dictionary<string, int>();

        public FolderPrecedence()
        {
        }

        public int GetFolderPriority(string dirName)
        {
            int p;
            if (priority.TryGetValue(dirName, out p))
                return p;
            else
                return 0;
        }

        /// <summary>
        /// Adds the relation "a > b" to the precedence table. If a 
        /// contradictory relation already exists, it is removed first.
        /// </summary>
        /// <param name="dirName1">First folder.</param>
        /// <param name="dirName2">Second folder.</param>
        public void SetRelation(string dirName1, string dirName2)
        {
            if (dirName1 == null)
                throw new ArgumentNullException("a");
            if (dirName2 == null)
                throw new ArgumentNullException("b");
            if (dirName1 == dirName2)
                throw new ArgumentException("Two items cannot be identical.");

            store[dirName1 + ">" + dirName2] = true;
            store.Remove(dirName2 + ">" + dirName1);
        }

#if false
        /// <summary>
        /// Returns true if at least one of the following is true:
        /// - a > the longest prefix, p of b such that a > p
        /// 
        /// - prefix_of(a) >
        /// - some_prefix_of(a) > some_prefix_of(b)
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        /// <remarks>
        /// Consider the example:
        /// /A1/A2 > /B1
        /// /B1/B2 > /A1
        /// Now we want to compare /A1/A2 and /B1/B2. Since /A1/A2 and /B1/B2
        /// is not explicitly defined, the result depends on which prefix of
        /// each element to use, and this is ambiguous. 
        /// </remarks>
        public bool GetRelation(string a, string b)
        {
        }
#endif

        public bool IsPreferred(string fileName1, string fileName2)
        {
            string key = string.Format("{0}>{1}",
                Path.GetDirectoryName(fileName1), Path.GetDirectoryName(fileName2));
            return store.ContainsKey(key);
        }

        /// <summary>
        /// Finds out the unique file name whose containing directory is
        /// preferred to the containing directory of any other file. If
        /// If no such file exists, returns -1.
        /// </summary>
        /// <param name="items"></param>
        /// <returns>Index of the unique preferred item.</returns>
        public int FindPreferredItem(IList<string> items)
        {
            for (int i = 0; i < items.Count; i++)
            {
                int j = 0;
                for (; j < items.Count; j++)
                {
                    if (j != i)
                    {
                        if (!IsPreferred(items[i], items[j]))
                            break;
                    }
                }
                if (j == items.Count)
                    return i;
            }
            return -1;
        }
    }
}
